import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import urllib


#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select("#srTable")

    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li.select("td")[1].text.strip()
        except:
            pass
        try:
            tempData["titleHref"] = 'https://www.jstor.org'+li.select(".title a")[0]["href"]
        except:
            pass
        try:
            tempData["author"] = [li.select(".contrib")[0].text.strip()]
        except:
            pass
        try:
            tempData["source"] = li.select(".src")[0].text.strip()
        except:
            pass
        allData.append(tempData)

    return allData

def getData(keyword):
    import requests

    cookies = {
        'XSRF-TOKEN': 'eba46d04-89ef-4392-98d3-f405f1bfac62',
        'cookie_current_cart': '',
        'SESSION': 'e254c0f5-766a-4451-9f36-e26a7b3e6cbc',
    }

    headers = {
        'Origin': 'http://www.cnbksy.com',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36',
        'Content-Type': 'application/json',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Referer': 'http://www.cnbksy.com/search',
        'X-Requested-With': 'XMLHttpRequest',
        'Connection': 'keep-alive',
    }

    params = (
        ('_csrf', 'eba46d04-89ef-4392-98d3-f405f1bfac62'),
    )

    data = '{"author":null,"clc":null,"searchContent":"cell","categories":"1,2,5","types":"1","isRestart":true,"pageCount":"10","asc":"false","relative":"true","facets":[]}'

    response = requests.post('http://www.cnbksy.com/search/query', headers=headers, params=params,data=data,cookies=cookies)

    print(response.text)
    exit()
    # NB. Original query string below. It seems impossible to parse and
    # reproduce query strings 100% accurately so the one below is given
    # in case the reproduced version is not "correct".
    # response = requests.post('http://www.cnbksy.com/search/query?_csrf=eba46d04-89ef-4392-98d3-f405f1bfac62', headers=headers, cookies=cookies, data=data)

    # NB. Original query string below. It seems impossible to parse and
    # reproduce query strings 100% accurately so the one below is given
    # in case the reproduced version is not "correct".
    # response = requests.get('http://www.cnbksy.com/search?searchContent=cell&categories=1,2&type=1', headers=headers)

    print(response.text)
    exit()

    allData = formData(response)

    return allData


def index():


    # page = 1
    data = json.loads(request.POST.get('data', '1'))

    allData= getData(data["values"][0])

    print(allData)
    # exit()




index()